package com.link.base.base.customeraccount.controller;


import cn.jiguang.common.utils.StringUtils;
import com.link.core.cllog.LCLogger;
import com.link.base.base.customeraccount.model.CustomerAccount;
import com.link.base.base.customeraccount.service.CustomerAccountService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;


/**
 * Copyright  hande Company. All Rights Reserved.
 * 参数设置[模块]
 *     客户参数
 *         账户启动参数设置
 * @author  unknwon
 * @date:   2018/11/9 15:41
 * @version 1.0
 */

@Controller
@RequestMapping("/link/customerAccount")
public class CustomerAccountController extends BasicController<CustomerAccount> {

    @Resource
    private CustomerAccountService customerAccountService;

    @Override
    public BasicService<CustomerAccount> getBasicService() throws Exception {
        return customerAccountService;
    }

    /***
     *  客户账户标签实体参数查询
     */
    @RequestMapping(value = "getCusAccntByParams", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getCusAccntByParams(@JsonParam CustomerAccount customerAccount) {

        Map<String, Object> result = new HashMap<String, Object>(4);
        List<CustomerAccount> list = null;
        try {
            list = customerAccountService.queryAll(customerAccount);
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<CustomerAccount>());
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 客户账户实体插入或更新
     * row_status  该参数默认更新，row_status=‘New’代表创建
     * 更新必须有id
     *
     * @TODO 组织id和职位id  是否需要绑定
     */
    @Override
    @RequestMapping(value = "upsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> upsert(@JsonParam CustomerAccount customerAccount, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        if (Objects.isNull(customerAccount) || StringUtils.isEmpty(customerAccount.getLabelName())) {
            result.put("success", false);
            result.put("result", new NullPointerException("传入数据异常").toString());
            return result;
        }
        try {
            result = super.upsert(customerAccount, session, request, response);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<CustomerAccount>());
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 插入客户账户标签
     *
     * @param customerAccount
     * @return
     */
    @Override
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insert(@RequestBody CustomerAccount customerAccount, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        if (Objects.isNull(customerAccount) || StringUtils.isEmpty(customerAccount.getLabelName())) {
            result.put("success", false);
            result.put("result", new NullPointerException("传入数据异常").toString());
            return result;
        }
        try {
            result = super.insert(customerAccount, session, request, response);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<CustomerAccount>());
            result.put("result", e.getMessage());
        }
        return result;
    }
}
